package com.marion.study.repo.write;

import com.marion.study.entity.UserAssetEntity;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

@Repository
public interface UserAssetWriteRepo extends JpaRepository<UserAssetEntity, Integer> {

    @Transactional(isolation = Isolation.READ_COMMITTED, propagation = Propagation.REQUIRED)
    @Modifying
    @Query(value = "update t_user_asset set amount = amount - :amount  where uid=:uid", nativeQuery = true)
    void reduceAmount(@Param("uid") long uid, @Param("amount") int amount);

}
